package com.cmos.um.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cmos.um.beans.CtiMessage;
import com.cmos.um.iservice.ICtiMessageSV;
import com.cmos.um.iservice.IHolidaySV;
import com.cmos.um.utils.RetMap;
import com.cmos.um.utils.WSContent;
import com.cmos.um.utils.WSRtnJsonCtx;
import com.cmos.um.utils.http.RestClientHttpRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;


@Service
public class HolidaySVImpl implements IHolidaySV {

    private final static Logger logger = LoggerFactory.getLogger(HolidaySVImpl.class);

    @Autowired
    private ICtiMessageSV ctiMessageservice;
    @Autowired
    private RestClientHttpRequest clien;

    /**
     * 节假日管理
     *
     * @param params
     * @return
     */
    @Override
    public Map<String, Object> synchHolidayInfo(JSONObject params) {

        JSONObject toCC = new JSONObject();//将要发送给cti的json消息
        List<CtiMessage> ctiMessage = ctiMessageservice.queryAll();
        if (ctiMessage.isEmpty()) {
            return new RetMap(logger, "尚未建设（和注册）任何的CC平台");
        }

        for (CtiMessage list : ctiMessage) {//遍历其中的cti每有一个cti则发送一次
            String ctiIp = list.getCtiIp();
            JSONObject jsonholidayparams = params.getJSONObject("params");
            String operation = jsonholidayparams.getString("operation");//操作类型   01：新增；02：修改；03：删除
            //遍历beans 每有一个节假日ID就向新方发送一次

            JSONArray listCtiJson = params.getJSONArray("beans");

            for (int J = 0; J < listCtiJson.size(); J++) {
                JSONObject ctiJson = listCtiJson.getJSONObject(J);

                String holidayId = ctiJson.getString("holidayId");//节假日Id
                String holiday = ctiJson.getString("holiday");//节假日
                toCC.put("holidayKey", holidayId);//假日键
                toCC.put("vccId", WSContent.companyNo);//集团号
                toCC.put("holidayName", holiday);//假日名称
                toCC.put("monthStart", 0);//开始月份
                toCC.put("monthEnd", 0);//结束月份
                toCC.put("dateStart", 0);//开始天数
                toCC.put("dateEnd", 0);//结束天数
                toCC.put("circle", 0);//循环   默认：0 0：每年循环 1：指定某年 yearCircle
                toCC.put("yearCircle", " ");//年份
                toCC.put("updateKey", " ");//更新标识
                JSONObject toCCFinal = null;
                String method = "operateHoliday";
                String toCCOperation = null;
                WSRtnJsonCtx wsRtnJsonCtx = new WSRtnJsonCtx();
                //跟据toCCOperation 决定调用接口
                if ("01".equals(operation)) {
                    toCCOperation = "0";
                }
                if ("02".equals(operation)) {
                    toCCOperation = "2";
                }
                if ("03".equals(operation)) {
                    toCCOperation = "1";
                }
                wsRtnJsonCtx = clien.doPost(WSContent.vccUrl, ctiIp, "operateHoliday", toCCOperation, toCC);
                if (!"0".equals(wsRtnJsonCtx.getRescode())) {
                    logger.error("调用cti节假日管理接口失败，返回的信息为:" + wsRtnJsonCtx.getResJsonObj());
                    return new RetMap("0", "调用cti节假日管理接口异常");
                }

            }
        }
        return new RetMap("成功");
    }
}
